#include "Search.h"
#include <json/json.h>
#include <filesystem>
#include <json/value.h>
#include <tuple>
#include <vector>
#include "PQUtil.h"

static std::string parseSearch(std::string& search){
    std::string ret ;
    auto len = search.size();
    auto index = 0;
    if(len ==0){
        return std::string(" 1 > 0 ");
    }
    int remaind = len-index;
    while(index < len){
        remaind = len-index;
        // !!path
        if(remaind >2 && search.at(index)=='!' && search.at(index+1)=='!' && search.at(index+2)!='!'){
            int tmp = index+2;
            while(tmp <len && search.at(tmp) != ' ')tmp++;
            if(ret.size()>0){ ret +=" and "; }
            ret += " path ilike '%"+search.substr(index+2,tmp)+"%' ";
            index = tmp;
        }
        // !filename
        else if(remaind >1  && search.at(index)=='!' && search.at(index+1)!='!' ){
            int tmp = index+1;
            while(tmp <len && search.at(tmp) != ' ')tmp++;
            if(ret.size()>0){ ret +=" and "; }
            ret += " name ilike '%"+search.substr(index+1,tmp)+"%' ";
            index = tmp;
        }else if(search.at(index)==' '){
            while(index <len && search.at(index) == ' ')index++;
        }
        // code or describe
        else if(search.at(index)!=' '){
            int tmp = index+1;
            while(tmp <len && search.at(tmp) != ' ')tmp++;
            if(ret.size()>0){ ret +=" and ";}
            ret += "( code ilike '%"+search.substr(index,tmp)+"%' ";
            ret += " or describe ilike '%"+search.substr(index,tmp)+"%') ";
            index = tmp;
        }
    }
    return ret;
}


void Search::getIndex(const HttpRequestPtr &req, std::function<void (const HttpResponsePtr &)> &&callback)
{
    auto search = req->getParameter("search");
    auto lang = req->getParameter("lang");
    std::string cond = parseSearch(search);

    auto page = req->getParameter("page");
    auto size = req->getParameter("size");
    auto type = req->getParameter("type");
    auto json = PQUtil::readData(lang, cond,page,size,type);
    auto resp = HttpResponse::newHttpJsonResponse(json);


  auto &origin = req->getHeader("Origin");
  if (origin.empty())
  {
      resp->addHeader("Access-Control-Allow-Origin", "*");
  }
  else
  {
      resp->addHeader("Access-Control-Allow-Origin", origin);
  }
  resp->addHeader("Access-Control-Allow-Methods", "GET,POST,OPTIONS");
  // resp->addHeader("Access-Control-Allow-Headers", headers);

    callback(resp);
}

void Search::getPage(const HttpRequestPtr &req, std::function<void (const HttpResponsePtr &)> &&callback)
{
    auto table = req->getParameter("table");
    auto path = req->getParameter("path");
    auto page = req->getParameter("page");
    auto json = PQUtil::searchPdfPage(path,table,page);
    auto resp = HttpResponse::newHttpJsonResponse(json);


  auto &origin = req->getHeader("Origin");
  if (origin.empty())
  {
      resp->addHeader("Access-Control-Allow-Origin", "*");
  }
  else
  {
      resp->addHeader("Access-Control-Allow-Origin", origin);
  }
  resp->addHeader("Access-Control-Allow-Methods", "GET,POST,OPTIONS");
  // resp->addHeader("Access-Control-Allow-Headers", headers);

    callback(resp);
}

void Search::addPage(const HttpRequestPtr &req, std::function<void (const HttpResponsePtr &)> &&callback)
{
    auto table = req->getParameter("table");
    auto path = req->getParameter("path");
    PQUtil::updateCppPdf(path,table);
    Json::Value json;
    json["error"] = 0;
    auto resp = HttpResponse::newHttpJsonResponse(json);


  auto &origin = req->getHeader("Origin");
  if (origin.empty())
  {
      resp->addHeader("Access-Control-Allow-Origin", "*");
  }
  else
  {
      resp->addHeader("Access-Control-Allow-Origin", origin);
  }
  resp->addHeader("Access-Control-Allow-Methods", "GET,POST,OPTIONS");
  // resp->addHeader("Access-Control-Allow-Headers", headers);

    callback(resp);
}

void Search::getToc(const HttpRequestPtr &req, std::function<void (const HttpResponsePtr &)> &&callback)
{
    auto table = req->getParameter("table");
    auto path = req->getParameter("path");
    auto cid = req->getParameter("cid");
    auto pcid = req->getParameter("pcid");
    auto json = PQUtil::searchPdfToc(path,table,cid,pcid);
    auto resp = HttpResponse::newHttpJsonResponse(json);


  auto &origin = req->getHeader("Origin");
  if (origin.empty())
  {
      resp->addHeader("Access-Control-Allow-Origin", "*");
  }
  else
  {
      resp->addHeader("Access-Control-Allow-Origin", origin);
  }
  resp->addHeader("Access-Control-Allow-Methods", "GET,POST,OPTIONS");
  // resp->addHeader("Access-Control-Allow-Headers", headers);

    callback(resp);
}

